﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Control;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastData;
using M6R小圆柱注液机.Data;

namespace M6R小圆柱注液机.Pages
{
    public partial class StartingCheck : Form
    {
        public StartingCheck()
        {
            InitializeComponent();
        }




        private void StartingCheck_Load(object sender, EventArgs e)
        {
            //资产编码
            string value = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "AssetsEncoding");
            tbAssetsEncoding.Text = value;
            tbAssetsEncoding.Enabled = false;
            AssetsEncoding_MFG(value);//进行资产编码的验证（设备号）
            //成品编码
            tbFinishedProductCoding.Text = "";
            this.Show();
            tbFinishedProductCoding.Focus();
            //电解液批次
            tbElectrolyteBatch.Text = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ElectrolyteBatch");
            btnElectrolyteVerify.Enabled = false;
            btnNext.Enabled = false;

        }



        /// <summary>
        /// 资产编码MFG验证
        /// </summary>
        private void AssetsEncoding_MFG(string value)
        {
            //先把控件里面的信息发送给MFG，待MFG返回以后，判断结果，并把结果写给 result
            string feedbackValue = GlobalValues.serialPort5.SendBackMessageInstant(value, 5000);

            if (feedbackValue == null)
            {
                GlobalValues.CommonVariable.mechineInfo = "-";
                GlobalValues.StatusVariable.isAssetsEncodingCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息超时，请确认！", "Alarm", true);
            }
            else if (feedbackValue.Contains("OK"))
            {
                GlobalValues.CommonVariable.mechineInfo = value;
                GlobalValues.StatusVariable.isAssetsEncodingCheckOutSuccess = true;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，资产编码验证成功", "Info", true);
            }
            else if (feedbackValue.Contains("NG"))
            {
                GlobalValues.CommonVariable.mechineInfo = "-";
                GlobalValues.StatusVariable.isAssetsEncodingCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，资产编码验证失败", "Alarm", true);
            }
            else
            {
                GlobalValues.CommonVariable.mechineInfo = "-";
                GlobalValues.StatusVariable.isAssetsEncodingCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息不合法，信息为：" + feedbackValue, "Alarm", true);
            }
        }

        /// <summary>
        /// 成品编码验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProductCodingVerify_Click(object sender, EventArgs e)
        {
            if (tbFinishedProductCoding.Text == "")
            {
                MessageBox.Show("成品编码输入不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbFinishedProductCoding.Focus();
                return;
            }

            //先把控件里面的信息发送给MFG，待MFG返回以后，判断结果，并把结果写给 result
            string feedbackValue = GlobalValues.serialPort5.SendBackMessageInstant(tbFinishedProductCoding.Text, 5000);

            if (feedbackValue == null)
            {
                lbProductCodingVerifyResult.Text = "验证超时";
                lbProductCodingVerifyResult.ForeColor = Color.Red;
                //传值
                GlobalValues.CommonVariable.finishedProductCodingInfo = "-";
                GlobalValues.StatusVariable.isFinishedProductCodingCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息超时，请确认！", "Alarm", true);
            }
            else if (feedbackValue.Contains("OK"))
            {
                lbProductCodingVerifyResult.Text = "验证成功";
                lbProductCodingVerifyResult.ForeColor = Color.Green;
                //传值
                GlobalValues.CommonVariable.finishedProductCodingInfo = tbFinishedProductCoding.Text;
                GlobalValues.StatusVariable.isFinishedProductCodingCheckOutSuccess = true;
                //保存到配置文件中
                SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "FinishedProductCoding", tbFinishedProductCoding.Text);
                //结果验证成功，解锁电解液批次的验证
                btnElectrolyteVerify.Enabled = true;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，成品编码验证成功", "Info", true);
            }
            else if (feedbackValue.Contains("NG"))
            {
                lbProductCodingVerifyResult.Text = "验证失败";
                lbProductCodingVerifyResult.ForeColor = Color.Red;
                //传值
                GlobalValues.CommonVariable.finishedProductCodingInfo = "-";
                GlobalValues.StatusVariable.isFinishedProductCodingCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，成品编码验证失败", "Alarm", true);
            }
            else
            {
                lbProductCodingVerifyResult.Text = "当前输入不合法";
                lbProductCodingVerifyResult.ForeColor = Color.Red;
                //传值
                GlobalValues.CommonVariable.finishedProductCodingInfo = "-";
                GlobalValues.StatusVariable.isFinishedProductCodingCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息不合法，信息为：" + feedbackValue, "Alarm", true);
            }

        }

        /// <summary>
        /// 电解液批次号验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnElectrolyteVerify_Click(object sender, EventArgs e)
        {
            //判断读取上来的电解液批次号的前几位是否与配置文件保存的一致？用于确认电解液批次号的正确性
            if (!tbElectrolyteBatch.Text.Contains(SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ElectrolyteBatchFrontSerialNumber")))
            {
                //读取上来的电解液批次号与配置文件保存的电解液前几位不一致
                MessageBox.Show("电解液批次号不正确，请检查！！！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                GlobalValues.log_Helper.DispProcess("电解液批次号不正确，请检查！！！", "Alarm", true);
                return;
            }

            //先把控件里面的信息发送给MFG，待MFG返回以后，判断结果，并把结果写给 result
            string feedbackValue = GlobalValues.serialPort5.SendBackMessageInstant(tbElectrolyteBatch.Text, 5000);

            if (feedbackValue == null)
            {
                lbElectrolyteVerifyResult.Text = "验证超时";
                lbElectrolyteVerifyResult.ForeColor = Color.Red;
                //传值
                GlobalValues.CommonVariable.electrolyteNumberInfo = "-";
                GlobalValues.StatusVariable.isElectrolyteNumberCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息超时，请确认！", "Alarm", true);
            }
            else if (feedbackValue.Contains("OK"))
            {
                lbElectrolyteVerifyResult.Text = "验证成功";
                lbElectrolyteVerifyResult.ForeColor = Color.Green;
                //传值
                GlobalValues.CommonVariable.electrolyteNumberInfo = tbElectrolyteBatch.Text;
                GlobalValues.StatusVariable.isElectrolyteNumberCheckOutSuccess = true;
                //将当前电解液批次号更新到配置文件中
                SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ElectrolyteBatch", tbElectrolyteBatch.Text);
                tbElectrolyteBatch.Enabled = false;
                //结果验证成功，解锁"下一步"控件
                btnNext.Enabled = true;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，电解液批次号验证成功", "Info", true);
            }
            else if (feedbackValue.Contains("NG"))
            {
                lbElectrolyteVerifyResult.Text = "验证失败";
                lbElectrolyteVerifyResult.ForeColor = Color.Red;
                //传值
                GlobalValues.CommonVariable.electrolyteNumberInfo = "-";
                GlobalValues.StatusVariable.isElectrolyteNumberCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，电解液批次号验证失败", "Alarm", true);
            }
            else
            {
                lbElectrolyteVerifyResult.Text = "当前输入不合法";
                lbElectrolyteVerifyResult.ForeColor = Color.Red;
                //传值
                GlobalValues.CommonVariable.electrolyteNumberInfo = "-";
                GlobalValues.StatusVariable.isElectrolyteNumberCheckOutSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息不合法，信息为：" + feedbackValue, "Alarm", true);
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChangeElectrolyteBatch_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认修改电解液的批次号？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                tbElectrolyteBatch.Enabled = true;
            }
        }
    }
}
